#!/bin/bash

function validate_num() {
    val_num=$1
    val_min=$2
    val_max=$3

    if [ -z "$val_num" ]; then
        echo "val num empty"
        return 0
    fi

    case $val_num in
        -*) ;;
        *[!0-9]*) return 0 ;;
    esac

    # 检查范围
    if [ "$val_num" -ge "$val_min" ] && [ "$val_num" -le "$val_max" ]; then
        echo "val num check ok"
        return 1
    else
        echo "val num check failed"
        return 0
    fi
}

if [ -z "$DB_DATABASE" ]; then
    echo  "please set quipus DB_DATABASE in ./env/db.env"
    return -1
fi
echo "quipus DB_DATABASE:"$DB_DATABASE

if [ -z "$DB_USERNAME" ]; then
    echo  "please set quipus DB_USERNAME in ./env/db.env"
    return -1
fi
echo "quipus DB_USERNAME:"$DB_USERNAME

if [ -z "$DB_PASSWORD" ]; then
    echo  "please set quipus DB_PASSWORD in ./env/db.env"
    return -1
fi
echo "quipus DB_USERNAME"$DB_PASSWORD

validate_num "$DB_PORT" 1024 5000
if validate_num "$DB_PORT" 1000 65535; then
    echo "error port $DB_PORT, not in range:1000~65535, default use 5432"
    DB_PORT="5432"
fi
echo "db DB_PORT:"$DB_PORT

if validate_num "$SSL_MODE" 0 1; then
    echo "error database ssl mode $SSL_MODE (0: disable, 1: enable), use default 0(disable)"
    SSL_MODE=0
fi

exit 0
